package com.dodo.blog.ui.page.publish;

import com.comvai.auth.model.LoginProvider;
import com.dodo.blog.WebApplication;
import com.dodo.blog.exception.BackendException;
import com.dodo.blog.model.Account;
import com.dodo.blog.model.Role;
import com.dodo.blog.server.AccountService;
import com.dodo.blog.ui.ajax.Response;
import com.dodo.blog.ui.ajax.annotation.GetModel;
import com.dodo.blog.ui.ajax.annotation.SetModel;
import com.dodo.blog.ui.component.composite.FieldSet;
import com.dodo.blog.ui.component.composite.FormRow;
import com.dodo.blog.ui.component.container.Form;
import com.dodo.blog.ui.component.input.AjaxSubmitButton;
import com.dodo.blog.ui.component.input.CheckBox;
import com.dodo.blog.ui.component.input.EnumChoiceRenderer;
import com.dodo.blog.ui.component.input.Select;
import com.dodo.blog.ui.component.input.TextBox;
import com.dodo.blog.ui.page.AbstractPage;
import com.dodo.blog.ui.util.ValidationUtil;

import javax.inject.Inject;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public class Registration
        extends AbstractPage
{
    private static final long serialVersionUID = 1L;

    private static AccountService accountService;

    private static final String REGISTRATION_FORM = "registration-form";

    @SetModel( name = REGISTRATION_FORM )
    public Response setModel( Account account )
    {
        try
        {
            if ( account.getLoginProvider() != null )
            {
                account.setLoginType( account.getLoginProvider().getLoginType() );
            }

            // set role
            account.setRole( Role.USER );

            // validate
            ValidationUtil.validateRegistration( account );

            // register account
            accountService.register( account );
        }
        catch ( BackendException e )
        {
            error( e );
        }

        return Response.redirect( WebApplication.get().getHomePage() );
    }

    @GetModel( name = REGISTRATION_FORM )
    public Account getModel()
    {
        return new Account();
    }

    @Override
    public void onRender()
    {
        setHeader( localize( "header.registration" ) );

        Form form = new Form( REGISTRATION_FORM, Account.class );
        add( form );

        FieldSet loginInfo = new FieldSet( localize( "title.loginInfo" ) );
        form.add( loginInfo );

        loginInfo.add( new FormRow( localize( "label.loginProvider" ), new Select<LoginProvider>( "loginProvider", LoginProvider.class.getEnumConstants(), new EnumChoiceRenderer() ) ) );
        loginInfo.add( new FormRow( localize( "label.email" ), new TextBox( "email" ) ) );
        loginInfo.add( new FormRow( localize( "label.login" ), new TextBox( "login" ) ) );
        loginInfo.add( new FormRow( localize( "label.password" ), new TextBox( "password" ) ) );
        loginInfo.add( new FormRow( localize( "label.passwordAgain" ), new TextBox( "passwordAgain" ) ) );

        FieldSet additionalInfo = new FieldSet( localize( "title.additionalInfo" ) );
        form.add( additionalInfo );

        additionalInfo.add( new FormRow( localize( "label.name" ), new TextBox( "name" ) ) );
        additionalInfo.add( new FormRow( localize( "label.surname" ), new TextBox( "surname" ) ) );
        additionalInfo.add( new FormRow( localize( "label.notify" ), new CheckBox( "notify" ) ) );

        // TODO: captcha

        form.add( new AjaxSubmitButton( "save", localize( "button.register" ) ) );
    }

    @Inject
    public void setAccountService( AccountService accountService )
    {
        Registration.accountService = accountService;
    }
}